/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.tweet;

import java.util.ArrayList;
import java.util.List;

import com.cenluan.me.call.Call;
import com.cenluan.tweet.comment.TweetComment;
import com.cenluan.tweet.photo.TweetPhoto;
import com.cenluan.user.User;
import com.cenluan.user.log.UserLog;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 微博
 * 
 * @author peak
 * 
 */
public class Tweet extends Model<Tweet> {

	private static final long serialVersionUID = 1L;

	public static final Tweet dao = new Tweet();

	/**
	 * 获取创建者
	 * 
	 * @return
	 */
	public User getCreater() {
		return User.dao.findById(getStr("createrId"));
	}

	/**
	 * 获取图片列表
	 * 
	 * @return
	 */
	public List<TweetPhoto> getPhotoList() {
		return TweetPhoto.dao.find("select * from t_tweet_photo"
				+ " where tweetId=? order by id asc", getStr("id"));
	}

	/**
	 * 获取照片数量
	 * 
	 * @return
	 */
	public long getPhotoCount() {
		return Db.queryLong("select count(*) from t_tweet_photo"
				+ " where tweetId=?", getStr("id"));
	}

	/**
	 * 获取评论列表
	 * 
	 * @return
	 */
	public List<TweetComment> getCommentList() {
		return TweetComment.dao.find("select * from t_tweet_comment "
				+ "where tweetId=? order by createTime desc", getStr("id"));
	}

	/**
	 * 获取评论的数量
	 * 
	 * @return
	 */
	public long getCommentCount() {
		return Db.queryLong("select count(*) from t_tweet_comment "
				+ "where tweetId=?", getStr("id"));
	}

	@Override
	public boolean delete() {
		// 删除照片和评论
		for (TweetComment comment : getCommentList()) {
			comment.delete();
		}
		for (TweetPhoto photo : getPhotoList()) {
			photo.delete();
		}
		// 删除关联的动态和通知
		UserLog.dao.deleteByReferenceId(getStr("id"));
		Call.dao.deleteByReferenceId(getStr("id"));
		return super.delete();
	}

	/**
	 * 分页查询微博
	 * 
	 * @param pn
	 * @param pz
	 * @param order
	 * @param createrId
	 * @return
	 */
	public Page<Tweet> page(int pn, int pz, String order, String createrId) {
		StringBuilder sql = new StringBuilder();
		List<Object> paras = new ArrayList<Object>();
		sql.append(" from t_tweet t ");
		if ("hot".equals(order)) {
			sql.append(" left join ( ");
			sql.append(" select count(*) ct,tweetId ");
			sql.append(" from t_tweet_comment ");
			sql.append(" group by tweetId ");
			sql.append(" ) s on s.tweetId=t.id ");
		}
		sql.append(" where 1=1 ");
		if (StrKit.notBlank(createrId)) {
			sql.append(" and t.createrId=? ");
			paras.add(createrId);
		}
		if ("hot".equals(order)) {
			sql.append(" order by s.ct desc,t.createTime desc ");
		} else {
			sql.append(" order by t.createTime desc ");
		}
		return paginate(pn, pz, "select t.* ", sql.toString(), paras.toArray());
	}

}
